package com.controller.library;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.action.library.TGetSelfPapersAction;
import com.bean.library.StatisticPaper;
import com.bean.library.Teacher;

public class TCourseSelectServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request,HttpServletResponse response){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String operateType = request.getParameter("operate_type");
		String courseId = request.getParameter("select_course");
		String courseName = request.getParameter("select_course_name");
		
		if(operateType.equals("setques")){
			request.getSession().setAttribute("selectedCourseId", courseId);
			request.getSession().setAttribute("selectedCourseName", courseName);
			try {
				response.sendRedirect("/Exam1/teacher/t_page/t_setques.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else if(operateType.equals("checkpaper")){
			request.getSession().setAttribute("selectedCourseId", courseId);
			request.getSession().setAttribute("selectedCourseName", courseName);
			Teacher teacher = (Teacher)request.getSession().getAttribute("teacher");
			String teacherId = teacher.getTeacherId();
			TGetSelfPapersAction tGetSelfPapersAction = new TGetSelfPapersAction();
			ArrayList<StatisticPaper>[] selfPapers = tGetSelfPapersAction.getselfPapers(teacherId, courseId);
			ArrayList<StatisticPaper> examPaperList = selfPapers[0];
			ArrayList<StatisticPaper> sparePaperList = selfPapers[1];
			ArrayList<StatisticPaper> modelPaperList = selfPapers[2];
			request.getSession().setAttribute("examPaperList", examPaperList);
			request.getSession().setAttribute("sparePaperList", sparePaperList);
			request.getSession().setAttribute("modelPaperList", modelPaperList);
			request.getSession().setAttribute("startIndex", 0);
			try {
				response.sendRedirect("/Exam1/teacher/t_page/t_checkpaper.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else{
			//正常情况不会执行到这里
		}
	}
}
